package com.unicorn.modual.fts.controller;

import com.unicorn.base.BaseController;
import com.unicorn.base.Routes;
import com.unicorn.entity.enums.SupportFileType;
import com.unicorn.modual.fts.service.FtsService;
import com.unicorn.util.JsonResponse;
import com.unicorn.vo.FileVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by user on 2016/12/4.
 * 在线文件管理系统
 */
@RestController
public class FtsContoller extends BaseController {

    @Autowired
    private FtsService ftsService;
    private static Logger logger= LoggerFactory.getLogger(FtsContoller.class);

    /**
     * 在线文件 列表
     * /system/online/files/list
     * @param fileVO
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping(value = Routes.SYSTEM_ON_LINE_FILE_LIST,method = RequestMethod.GET)
    public JsonResponse listOnlineFiles(FileVO fileVO, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String contextPath=getBasePath();
        System.out.println("contextPath----->"+contextPath);
        List<FileVO> files=ftsService.listFiles(fileVO.getParentPath());

        return requestHandler.handleRequest(files,request,response, logger);

    }

    /**
     * 创建在线文件/文件夹
     * /system/online/files/create
     * @param fileVO
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping(value = Routes.SYSTEM_ON_LINE_FILE_CREATE,method = RequestMethod.POST)
    public JsonResponse createFile(@RequestBody FileVO fileVO, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String contextPath=request.getServletContext().getRealPath("/");
        System.out.println("contextPath----->"+contextPath);

        FileVO file = ftsService.createFileOrDirectory(fileVO.getFileType(), fileVO.getFileName(), fileVO.getParentPath());

        return requestHandler.handleRequest(file,request,response, logger);

    }

    /**
     * 删除在线文件/文件夹
     * /system/online/files/delete
     * @param fileVO
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping(value = Routes.SYSTEM_ON_LINE_FILE_DELETE,method = RequestMethod.DELETE)
    public JsonResponse deleteFile(@RequestBody FileVO fileVO, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String contextPath=request.getServletContext().getRealPath("/");
        System.out.println("contextPath----->"+contextPath);

        boolean isSuccess = ftsService.deleteFileOrDirectory(fileVO.getFileName(), fileVO.getParentPath());

        return requestHandler.handleRequest(isSuccess,request,response, logger);

    }




}
